//
//  JRBaseTableViewController.swift
//  LEDOTA
//
//  Created by rui on 2024/4/12.
//

import UIKit

open class JRBaseTableViewController: JRBaseVC {
        
    public enum JRTableViewSectionKeys: String {
        case rows = "rows"
        case indentifierForHeaderInSection = "indentifierForHeaderInSection"
        case backgroundColorForHeaderInSection = "backgroundColorForHeaderInSection"
        case backgroundColorForHeaderContentViewInSection = "backgroundColorForHeaderContentViewInSection"

        case indentifierForFooterInSection = "indentifierForFooterInSection"
        case backgroundColorForFooterInSection = "backgroundColorForFooterInSection"
        case backgroundColorForFooterContentViewInSection = "backgroundColorForFooterContentViewInSection"

        case heightForHeaderInSection = "heightForHeaderInSection"
        case heightForFooterInSection = "heightForFooterInSection"
        public enum JRTableViewRowKeys: String {
            case indentifier = "idnetifier"
            case `height` = "height"
            case backgroundColorForCellInContentView = "backgroundColorForCellInContentView"
            case backgroundColorForCell = "backgroundColorForCell"
            case `data` = "data"
            case clousre = "clousre"
        }
    }
    
    public typealias JRTableViewClosure = ()->Void
    
    public typealias JRTableViewRowKeyValues = [JRTableViewSectionKeys.JRTableViewRowKeys: Any]

    public typealias JRTableViewSectionKeyValues = [JRTableViewSectionKeys: Any]
    
    public convenience init(style: UITableView.Style) {
        self.init()
        self.style = style
    }
    
    open lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRectZero, style: self.style)
        self.view .addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self.view)
            make.top.equalTo(self.view.snp_topMargin)
        }
        return tableView
    }()
    
    private var style: UITableView.Style = .plain
    
    open var dataSources: [JRTableViewSectionKeyValues]? {
        didSet {
            self.tableView.reloadData()
        }
    }

    open override func viewDidLoad() {
        super.viewDidLoad()
        JRTableViewCell.register(tableView)
        tableView.register(JRTableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: JRTableViewHeaderFooterView.identifier)
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0.0
        }
        tableView.separatorColor = UIColor.clear
        tableView.separatorStyle = .none
        tableView.separatorInset = .zero
        tableView.estimatedRowHeight = 44.0
        tableView.estimatedSectionFooterHeight = 0.0
        tableView.estimatedSectionHeaderHeight = 0.0
        tableView.tableFooterView = UIView()
        tableView.tableHeaderView = UIView()
        tableView.contentInsetAdjustmentBehavior = .never
    }
}

extension JRBaseTableViewController: UITableViewDelegate, UITableViewDataSource {
    open func numberOfSections(in tableView: UITableView) -> Int {
        let num = self.dataSources?.count ?? 0
        return num
    }
    open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let sectionDic: JRTableViewSectionKeyValues? = self.dataSources?[section] as? JRTableViewSectionKeyValues
        let list = sectionDic?[.rows] as? [Any]
        return list?.count ?? 0
    }
    open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let sectionDic: JRTableViewSectionKeyValues? = self.dataSources?[indexPath.section] as? JRTableViewSectionKeyValues
        let list: [JRTableViewRowKeyValues]? = sectionDic?[.rows] as? [JRTableViewRowKeyValues]
        let rowDic = list?[indexPath.row] as? JRTableViewRowKeyValues
        let identifier: String = (rowDic?[.indentifier] as? String) ?? JRTableViewCell.identifier
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier) ?? JRTableViewCell(reuseIdentifier: JRTableViewCell.identifier)
        if cell.isKind(of: JRTableViewCell.self) {
            let aCell = cell as! JRTableViewCell
            aCell.setCellData(rowDic?[.data])
        }
        if rowDic?.keys.contains(.backgroundColorForCell) ?? false {
            let color = (rowDic?[.backgroundColorForCell] as? UIColor) ?? .white
            if #available(iOS 14.0, *) {
                cell.backgroundConfiguration?.backgroundColor = color
            } else {
                cell.backgroundColor = color
            }

            cell.backgroundColor = (rowDic?[.backgroundColorForCell] as? UIColor) ?? .white
        }
        if rowDic?.keys.contains(.backgroundColorForCellInContentView) ?? false {
            let color = (rowDic?[.backgroundColorForCellInContentView] as? UIColor) ?? .white
            cell.contentView.backgroundColor = color
        }
        return cell
    }
    open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let sectionDic: JRTableViewSectionKeyValues? = self.dataSources?[indexPath.section] as? JRTableViewSectionKeyValues
        let list: [JRTableViewRowKeyValues]? = sectionDic?[.rows] as? [JRTableViewRowKeyValues]
        let rowDic = list?[indexPath.row] as? JRTableViewRowKeyValues
        guard let closure: JRTableViewClosure = rowDic?[.clousre] as? JRTableViewClosure else { return }
        closure()
    }
    open func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let sectionDic: JRTableViewSectionKeyValues? = self.dataSources?[indexPath.section] as? JRTableViewSectionKeyValues
        let list: [JRTableViewRowKeyValues]? = sectionDic?[.rows] as? [JRTableViewRowKeyValues]
        let rowDic = list?[indexPath.row] as? JRTableViewRowKeyValues
        let height = (rowDic?[.height] as? CGFloat) ?? 44.0
        return height
    }
    open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionDic: JRTableViewSectionKeyValues? = self.dataSources?[section] as? JRTableViewSectionKeyValues
        let identifier: String = (sectionDic?[.indentifierForHeaderInSection] as? String) ?? JRTableViewHeaderFooterView.identifier
        let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier) ?? JRTableViewHeaderFooterView(reuseIdentifier: JRTableViewHeaderFooterView.identifier)
        if sectionDic?.keys.contains(.backgroundColorForHeaderInSection) ?? false {
            let color = (sectionDic?[.backgroundColorForHeaderInSection] as? UIColor) ?? UIColor.white
            if #available(iOS 14.0, *) {
                var configuration = UIBackgroundConfiguration.clear()
                configuration.backgroundColor = color
                headerView.backgroundConfiguration = configuration
            } else {
                headerView.backgroundColor = color
            }
        }
        if sectionDic?.keys.contains(.backgroundColorForHeaderContentViewInSection) ?? false {
            let color = (sectionDic?[.backgroundColorForHeaderContentViewInSection] as? UIColor) ?? UIColor.white
            headerView.contentView.backgroundColor = color
        }
        return headerView
    }
    
    open func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        let sectionDic: JRTableViewSectionKeyValues? = self.dataSources?[section] as? JRTableViewSectionKeyValues
        guard let height = sectionDic?[.heightForHeaderInSection] as? Double else { return 0.0 }
        return CGFloat(height)
    }
    
    open func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let sectionDic: JRTableViewSectionKeyValues? = self.dataSources?[section] as? JRTableViewSectionKeyValues
        let identifier: String = (sectionDic?[.indentifierForFooterInSection] as? String) ?? JRTableViewHeaderFooterView.identifier
        let footerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier) ?? JRTableViewHeaderFooterView(reuseIdentifier: JRTableViewHeaderFooterView.identifier)
        if sectionDic?.keys.contains(.backgroundColorForHeaderInSection) ?? false {
            let color = (sectionDic?[.backgroundColorForHeaderInSection] as? UIColor) ?? UIColor.white
            if #available(iOS 14.0, *) {
                var configuration = UIBackgroundConfiguration.clear()
                configuration.backgroundColor = color
                footerView.backgroundConfiguration = configuration
            } else {
                footerView.backgroundColor = color
            }
        }
        if sectionDic?.keys.contains(.backgroundColorForFooterContentViewInSection) ?? false {
            let color = (sectionDic?[.backgroundColorForFooterContentViewInSection] as? UIColor) ?? UIColor.white
            footerView.contentView.backgroundColor = color
        }
        return footerView
    }
    open func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        let sectionDic: JRTableViewSectionKeyValues? = self.dataSources?[section] as? JRTableViewSectionKeyValues
        guard let height = sectionDic?[.heightForFooterInSection] as? Double else { return 0.0 }
        return CGFloat(height)
    }
}
